package com.xiaojie.hystrix.decoder;

import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import jdk.nashorn.internal.runtime.regexp.joni.exception.InternalException;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wangye
 * @classname MyErrorDecoder
 * @date 2021/6/21 10:37
 **/
@Slf4j
public class MyFeignErrorDecoder implements ErrorDecoder {

    @Override
    public Exception decode(String methodKey, Response response) {
        try {
            String body = null;
            if (response.body() != null) {
                body = Util.toString(response.body().asReader());
            }
            return new MyFeignException(response.status(), body);
        } catch (Exception e) {
            log.error(e.getMessage());
            return new InternalException(e.getMessage());
        }
    }
}
